package org.pine.controller;

import org.pine.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ ClassName BookController
 * @ Description: Redis 控制器测试
 * @ Author lzs
 * @ Date 2021-01-28
 * @ Version V1.0
 */
@RestController
public class BookController {
    RedisTemplate redisTemplate;
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @GetMapping("/testCluster")
    public void testCluster(){
        ValueOperations ops=redisTemplate.opsForValue();
        Book book=new Book(null,"水浒传","施耐庵");
        ops.set("b1",book);
        System.out.println(ops.get("b1"));
        ValueOperations<String,String> ops2=stringRedisTemplate.opsForValue();
        ops2.set("k1","v1");
        System.out.println(ops2.get("k1"));
    }

    @GetMapping("/testRedis")
    public void testRedis(){
        ValueOperations<String,String> ops1=stringRedisTemplate.opsForValue();
        ops1.set("name","三国演义");
        String name=ops1.get("name");
        System.out.println(name);

        ValueOperations<String,Object> ops2=redisTemplate.opsForValue();
        Book b1=new Book();
        b1.setId(2);
        b1.setName("三国演义");
        b1.setAuthor("罗贯中");
        ops2.set("book",b1);

        Book book= (Book) ops2.get("book");
        System.out.println("get book from redis"+book);

    }
}
